//
//  DRMessageVC.m
//  DoctorRen
//
//  Created by Macx on 2018/11/28.
//

#import "DRMessageVC.h"
#import "DRMessageCell.h"
#import "DRHttpModel.h"
#import <MJRefresh/MJRefresh.h>
#import "DRHomeDataModel.h"

@interface DRMessageVC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;
@property (weak, nonatomic) IBOutlet UITableView *questionTableView;
@property (weak, nonatomic) IBOutlet UITableView *systemTableView;

@property (nonatomic, strong) DRHttpModel *questionRequest;

@property (nonatomic, strong) DRHttpModel *systemRequest;

@property (nonatomic, strong) NSMutableArray *questionArray;

@property (nonatomic, strong) NSMutableArray *systemArray;

@end

@implementation DRMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.questionTableView.estimatedRowHeight = 130;
    self.questionTableView.rowHeight = UITableViewAutomaticDimension;
    self.systemTableView.estimatedRowHeight = 130;
    self.systemTableView.rowHeight = UITableViewAutomaticDimension;
    self.systemTableView.tableFooterView = [UIView new];
    self.questionTableView.tableFooterView = [UIView new];
    WEAKSELF
    self.questionTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadQuestionMessage];
    }];
    
    self.systemTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadSystemMessage];
    }];
    [SVProgressHUD showWithStatus:@"正在加载"];
    [self loadSystemMessage];
    [self loadQuestionMessage];
    
    [self.systemTableView setHidden:YES];
    [self.questionTableView setHidden:NO];
    [self setKefuButtonHidden:YES];

}



-(void) loadSystemMessage{
    [DRHomeDataModel loadMessage:self.systemRequest andSuccess:^(NSArray<DRMessageModel *> * _Nonnull models) {
        [self.systemTableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
        [self.systemArray removeAllObjects];
        [self.systemArray addObjectsFromArray:models];
        [self.systemTableView reloadData];
    } andFail:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}

-(void) loadQuestionMessage{
    [DRHomeDataModel loadMessage:self.questionRequest andSuccess:^(NSArray<DRMessageModel *> * _Nonnull models) {
        [self.questionTableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
        [self.questionArray removeAllObjects];
        [self.questionArray addObjectsFromArray:models];
        [self.questionTableView reloadData];
    } andFail:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}

- (IBAction)goback:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (IBAction)clickSegment:(UISegmentedControl *)sender {
    [self.questionTableView setHidden: sender.selectedSegmentIndex == 1];
    [self.systemTableView setHidden:sender.selectedSegmentIndex == 0];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.systemTableView]) {
        return self.systemArray.count;
    }else if([tableView isEqual:self.questionTableView]){
        return self.questionArray.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DRMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DRMessageCell"];
    if (!cell) {
        cell = [[DRMessageCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"DRMessageCell"];
    }
    if ([tableView isEqual:self.systemTableView]) {
        [cell setData:self.systemArray[indexPath.row]];
    }else if([tableView isEqual:self.questionTableView]){
        [cell setData:self.questionArray[indexPath.row]];
    }
    return cell;
}

-(DRHttpModel *)questionRequest{
    if (!_questionRequest) {
        _questionRequest = [[DRHttpModel alloc] init];
        _questionRequest.params = @{@"type": @"2",@"token": [DRLoginDataModel getUserToken]};
        _questionRequest.url = DRURL(@"index.php/NewIndex/system_index");
    }
    return _questionRequest;
}

-(DRHttpModel *)systemRequest{
    if (!_systemRequest) {
        _systemRequest = [[DRHttpModel alloc] init];
        _systemRequest.params = @{@"type": @"1",@"token": [DRLoginDataModel getUserToken]};
        _systemRequest.url = DRURL(@"index.php/NewIndex/system_index");
    }
    return _systemRequest;
}

-(NSMutableArray *)systemArray{
    if (!_systemArray) {
        _systemArray = [NSMutableArray array];
    }
    return _systemArray;
}

-(NSMutableArray *)questionArray{
    if (!_questionArray) {
        _questionArray = [NSMutableArray array];
    }
    return _questionArray;
}

@end
